﻿using DXBase;
using DXBase.D3D;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DXBase.Async;
using DXBase.Scenes;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using DXBase.Samples;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Sample0_DXCoreWindow
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
			LaunchScene();
        }

		async private void LaunchScene()
		{
			var timer = new PerformanceTimer();
			context = new DXContext();

			simulation = await Universes.CreateUniverse1(context);
			context.Target = new DXTargetSwapPanelOrWindow(GetDXRoot());

			CompositionTarget.Rendering += (o, e) =>
			{
				timer.Update();
				simulation.Update(context, timer);
				context.Rendering = true;
				simulation.Render(context);
				context.Rendering = false;
			};
		}
		Universe simulation;
		DXContext context;

		public SwapChainBackgroundPanel GetDXRoot() { return DXPanel; }

		private async void DoSave(object sender, RoutedEventArgs e) { await context.SavePic(asyncLocker); }
		AsyncLock asyncLocker = new AsyncLock();
	}
}
